package com.zhangyj.plugin;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.zhangyj.db.Database;

/**
 * 商品存货计算
 * 
 * @author zhangyj
 * 
 */
public class StorageChange extends StorageBase {

	Database dbc;
	GoodsLiuShuiHz hz;
	List<GoodsLiuShuiMx> mxs;
	GoodsLiuShuiMx mx;
	String costOrgId;
	/** 货主Id */
	String ownerid;
	/** 库存金额为零是否自动补出库成本 */
	boolean isbuckcb = false;

	public StorageChange() {
	}

	public void intiParameters() throws Exception {
	}

	public String execute() throws ComponentException {
		try {
			intiParameters();
			// TODO 没有实现 costOrgId = getCostOrgId();
			// TODO 总控.库存金额为零是否自动补出库成本='Y'
			if ("Y".equals("Y")) {
				isbuckcb = true;
			}
			for (Iterator<GoodsLiuShuiMx> iter = mxs.iterator(); iter.hasNext();) {
				mx = iter.next();
				processMx();
			}

			return null;
		} catch (Exception e) {
			throw new ComponentException(e);
		}
	}

	private void processMx() throws Exception {
		boolean guanlikucun = false; // var_是否管理库存 := 'N'
		boolean duoweiduguanli = false;// var_是否管理多维度 := 'N'
		String sql = "SELECT iskc,iswd FROM shpattr WHERE shpid=?";
		PreparedStatement ps = dbc.prepareStatement(sql);
		ps.setString(1, mx.getSpid());
		ResultSet rst = ps.executeQuery();
		if (rst.next()) {
			guanlikucun = "Y".equals(rst.getString(1));
			duoweiduguanli = "Y".equals(rst.getString(2));
		}
		rst.close();
		ps.close();
		if (!guanlikucun)
			return;
		boolean isxfwd = false;// 货位管理维度
		boolean isbaozh = false;// 分包装管理
		double chukshl = mx.getCukushul();// getDoubleValue(FIELD_INDEX_CUKUSHUL);
		double rukushl = mx.getRukushul();// getDoubleValue(FIELD_INDEX_RUKUSHUL);
		// (入库数量!=0) or (出库数量!=0)
		if (!isZore(chukshl) || isZore(rukushl)) {
			sql = "SELECT isxfwd,isbaozh FROM hwsp WHERE hwid=?";
			ps = dbc.prepareStatement(sql);
			ps.setString(1, mx.getHuowei());
			rst = ps.executeQuery();
			if (rst.next()) {
				isxfwd = "Y".equals(rst.getString(1));
				isbaozh = "Y".equals(rst.getString(2));
			}
			rst.close();
			ps.close();
		}
		String hwid = mx.getHuowei();

		double jcParams[][] = new double[4][];
		jcParams[0] = getJcParameters(dbc, hwid, mx.getSpid(), 0);// 货位结存
		jcParams[1] = getJcParameters(dbc, hz.getOrgid(), mx.getSpid(), 1);// 机构结存
		jcParams[2] = getJcParameters(dbc, costOrgId, mx.getSpid(), 2);// 核算中心机构结存
		jcParams[3] = getJcParameters(dbc, hz.getEntid(), mx.getSpid(), 3);// 总结存

		if (duoweiduguanli) {
			if (isxfwd) {
				if ("Y".equals(hz.getIszidfent()) && maxZore(chukshl)) {

					if (mx.getWeiduid() == null || mx.getWeiduid().length() == 0) {
						sql = "SELECT wdid, kcshl FROM shpwdjc WHERE hwid=? AND shpid=?";
						ps = dbc.prepareStatement(sql);
						ps.setString(1, hwid);
						ps.setString(2, mx.getSpid());
						rst = ps.executeQuery();
						List<Object[]> list = new ArrayList<Object[]>();
						if (rst.next()) {
							list.add(new Object[] { rst.getString(1), rst.getInt(2) });
						}
						rst.close();
						ps.close();
						double v_fentshl = mx.getCukushul();
						for (Iterator<Object[]> iterator = list.iterator(); iterator.hasNext();) {
							Object val[] = iterator.next();
							double kcshl = (Double) val[1];
							if (kcshl > v_fentshl) {
								update(dbc, (String) val[0], v_fentshl, mx.getSpid(), hwid, false);
								insertAutopihao(dbc, mx.getSpid(), hwid, mx.getWeiduid(), v_fentshl);
								v_fentshl = 0d;
								break;
							} else {
								update(dbc, (String) val[0], kcshl, mx.getSpid(), hwid, true);
								insertAutopihao(dbc, mx.getSpid(), mx.getHuowei(), mx.getWeiduid(), kcshl);
								v_fentshl = v_fentshl - kcshl;
							}
						}
						if (maxZore(v_fentshl) && list.size() > 0) {
							Object val[] = list.get(0);
							update(dbc, (String) val[0], v_fentshl, mx.getSpid(), hwid, false);
							insertAutopihao(dbc, mx.getSpid(), hwid, mx.getWeiduid(), v_fentshl);
						}
					} else {

						sql = "SELECT COUNT(*) FROM shpwdjc WHERE shpid=? AND wdid=? AND hwid=?";
						ps = dbc.prepareStatement(sql);
						ps.setString(1, mx.getSpid());
						ps.setString(2, mx.getWeiduid());
						ps.setString(3, mx.getHuowei());
						rst = ps.executeQuery();
						boolean has = false;
						if (rst.next()) {
							if (rst.getInt(1) > 0)
								has = true;
						}
						rst.close();
						ps.close();
						if (!has) {
							insertShpwdjc(dbc, mx.getSpid(), hwid, mx.getWeiduid(), mx.getDate(), mx.getRukudanjia(), mx.getRukujianshu(), mx.getRukushul());
						} else {
							updateShpwdjc(dbc, mx.getSpid(), hwid, mx.getWeiduid(), mx.getRukujianshu(), mx.getCukujianshu(), mx.getCukushul());
						}
					}
				}
			}
		}
		// 分包装管理
		if (isbaozh) {
			String baozhid = mx.getBaozhid();
			sql = "SELECT spid FROM shpbzjc WHERE shpid=? AND hwid=? AND baozhid=? and ownerid=?";
			ps = dbc.prepareStatement(sql);
			ps.setString(1, mx.getSpid());
			ps.setString(2, hwid);
			ps.setString(3, baozhid);
			ps.setString(4, ownerid);
			rst = ps.executeQuery();
			boolean has = false;
			if (rst.next()) {
				has = true;
			}
			rst.close();
			ps.close();
			if (!has) {
				insertShpbzjc(dbc, mx.getSpid(), hwid, baozhid);
			} else {
				updateShpbzjc(dbc, mx.getSpid(), hwid, baozhid);
			}
		}

		if (maxZore(rukushl) && (hz.getYwbs() == YWBS_CAI_GOU || hz.getYwbs() == YWBS_WAI_DIAO_RU_KU)) {
			double rukudj = mx.getRukudanjia();// getDoubleValue(FIELD_INDEX_RUKUDANJIA);
			for (int i = 0; i < jcParams.length; i++) {
				if (maxZore(jcParams[i][0] * jcParams[i][1])) {
					jcParams[i][3] = jcParams[i][1] / jcParams[i][0];
				}
				if (i == 0)
					continue;
				if (rukudj > jcParams[i][4]) {
					jcParams[i][4] = rukudj;
				}
				if (rukudj < jcParams[i][5]) {
					jcParams[i][5] = rukudj;
				}
				if (maxZore(rukudj)) {
					jcParams[i][6] = rukudj;
				}
			}
		}
		updatejc(dbc, hwid, mx.getSpid(), jcParams[0], 4, 0);
		updatejc(dbc, hz.getOrgid(), mx.getSpid(), jcParams[1], jcParams[1].length, 1);
		if (costOrgId != null && costOrgId.length() > 0)
			updatejc(dbc, costOrgId, mx.getSpid(), jcParams[2], jcParams[2].length, 2);
		updatejc(dbc, hz.getEntid(), mx.getSpid(), jcParams[3], jcParams[3].length, 3);
		insertShpzymx(dbc, jcParams, hz.getZhyid(), mx.getSpid(), hwid, mx.getWeiduid());

		// if 业务标识 in ('采购','外调入库')
		/**
		 * var_成本核算方式ID 怎么得到现在还不知道
		 */
		int ywbs = hz.getYwbs();
		if (maxZore(rukushl) && (ywbs == YWBS_CAI_GOU || ywbs == YWBS_WAI_DIAO_RU_KU)) {
			int hesidCode = getHsfs();
			if (hesidCode == PI_CI_HE_SUAN) {
				insertShppici(dbc, costOrgId, mx.getSpid(), mx.getWeiduid(), hwid);
			}
			// if (var_成本核算方式ID in
			// ('先进先出','后进先出','高进先出','低进先出','本期先进先出','本期后进先出'))
			if ((hesidCode & 0x3F) == hesidCode) {
				insertRukuxul(dbc, costOrgId, mx.getSpid(), mx.getWeiduid(), hwid);
			}
		}
		if (this.isbuckcb) {
			double kcshl = 0d;
			double kcje = 0d;
			sql = "SELECT kcshl,kcje FROM sphwjc WHERE hwid=? and shpid=? AND ownerid=?";
			ps = dbc.prepareStatement(sql);
			ps.setString(0, hwid);
			ps.setString(1, mx.getSpid());
			ps.setString(2, ownerid);
			rst = ps.executeQuery();
			if (rst.next()) {
				kcshl = rst.getDouble(1);
				kcje = rst.getDouble(2);
			}
			rst.close();
			ps.close();
			// var_库存数量=0 and var_库存金额<>0
			if (isZore(kcshl) && !isZore(kcje)) {
				for (int i = 0; i < jcParams.length; i++) {
					jcParams[i][2] = jcParams[i][2] - kcje;
				}
				updateCkje(dbc, jcParams[0][2], 0, hwid, mx.getSpid());
				updateCkje(dbc, jcParams[1][2], 1, hz.getOrgid(), mx.getSpid());
				updateCkje(dbc, jcParams[2][2], 2, costOrgId, mx.getSpid());
				updateCkje(dbc, jcParams[3][2], 3, hz.getEntid(), mx.getSpid());
				insertShpzymx2(dbc, jcParams, kcje, hz.getZhyid(), mx.getSpid(), hwid, mx.getWeiduid());
			}
		}
	}

	private void updateCkje(Database dbc, double kcje, int type, String param, String shpid) throws Exception {
		String sql;
		switch (type) {
		case (0):
			sql = "UPDATE shphwjc SET kcje=? WHERE hwid=? AND shpid AND ownerid=?";
			break;
		case (1):
			sql = "UPDATE shporgjc SET kcje=? WHERE orgid=? AND shpid";
			break;
		case (2):
			sql = "UPDATE shphszx SET kcje=? WHERE orgid=? AND shpid";
			break;
		case (3):
			sql = "UPDATE shpentjc SET kcje=? WHERE entid=? AND shpid";
			break;
		default:
			return;
		}
		PreparedStatement ps = dbc.prepareStatement(sql);
		ps.setDouble(1, kcje);
		ps.setString(2, param);
		ps.setString(3, shpid);
		if (type == 0) {
			ps.setString(4, ownerid);
		}
		ps.executeUpdate();
	}

	private void insertRukuxul(Database dbc, String costorgid, String shpid, String wdid, String hwid) throws Exception {
		String sql = "INSERT rukuxul(serialnum,entid,orgid,costorgid,billno,billcode,prefix,dates,businessid,shpid,billsn,rukshl,rukdj,chkshl,wdid,hwid) VALUES (?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?)";
		int serialnum = 0;
		PreparedStatement ps = dbc.prepareStatement(sql);
		int index = 1;
		ps.setInt(index++, serialnum);// serialnum 排列号id
		ps.setString(index++, hz.getEntid());// entid 企业id
		ps.setString(index++, hz.getOrgid());// orgid 机构id
		ps.setString(index++, costorgid);// costorgid 成本中心id
		ps.setInt(index++, hz.getBillno());// billno 单据id
		ps.setString(index++, hz.getBillcode());// billcode 单据编号
		ps.setString(index++, hz.getYwbs_description());// prefix 业务标识
		ps.setString(index++, hz.getDate());// dates 日期
		ps.setString(index++, hz.getBusinessId());// businessid 单位id
		ps.setString(index++, shpid);// shpid 商品id
		ps.setInt(index++, mx.getBillsn());// billsn 单据序号
		ps.setDouble(index++, mx.getRukushul());// rukshl
		// 入库数量
		ps.setDouble(index++, mx.getRukudanjia());// rukdj
		// 入库单价
		ps.setDouble(index++, mx.getCukushul());// chkshl
		// 出库数量
		ps.setString(index++, wdid);// wdid 维度id
		ps.setString(index++, hwid);// hwid 存储位置id
		ps.execute();
		ps.close();
	}

	private void insertShppici(Database dbc, String costorgid, String shpid, String wdid, String hwid) throws Exception {
		String sql = "INSERT shppici (serialnum,entid,shpid,billsn,orgid,costorgid,dates,billno,billcode,shl,jj,wdid,hwid) VALUES(?,?,?,?,?, ?,?,?,?,?, ?,?,?)";
		int serialnum = 0;
		PreparedStatement ps = dbc.prepareStatement(sql);
		int index = 1;
		ps.setInt(index++, serialnum);// serialnum 排列号id
		ps.setString(index++, hz.getEntid());// entid 企业id
		ps.setString(index++, shpid);// shpid 商品id
		ps.setInt(index++, mx.getBillsn());// billsn 单据序号
		ps.setString(index++, hz.getOrgid());// orgid 机构id
		ps.setString(index++, costorgid);// costorgid 成本中心id
		ps.setString(index++, hz.getDate());// dates 日期
		ps.setInt(index++, hz.getBillno());// billno 单据id
		ps.setString(index++, hz.getBillcode());// billcode 单据编号
		ps.setDouble(index++, mx.getRukushul());// shl 采购数量
		ps.setDouble(index++, mx.getRukudanjia());// jj 采购单价
		ps.setString(index++, wdid);// wdid 维度id
		ps.setString(index++, hwid);// hwid 存储位置id
		ps.execute();
		ps.close();
	}

	private void insertShpzymx2(Database dbc, double jcParams[][], double kcje, int zhyid, String shpid, String hwid, String wdid) throws Exception {
		String sql = "INSERT shpzymx (hyid,serialnum,entid,shpid,hwid,wdid,billsn,summaries,rukjs,rukshl,rkdj,rkje,chkjs,chkshl,chkje,hwjcjs,hwjcshl,hwjcje,hwjcdj,orgjcjs,orgjcshl,orgjcje,orgjcdj,jianshu,kcshl,kcje,jcdj,hsje,je,lshje,maol,remark) VALUES (?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?)";
		double hwjcdj = 0d;
		double orgjcdj = 0d;
		double entjcdj = 0d;
		if (!isZore(jcParams[0][1])) {
			hwjcdj = jcParams[0][2] / jcParams[0][1];
		}
		if (!isZore(jcParams[1][1])) {
			orgjcdj = jcParams[1][2] / jcParams[1][1];
		}
		if (!isZore(jcParams[3][1])) {
			entjcdj = jcParams[3][2] / jcParams[1][1];
		}
		int serialnum = 0;
		int index = 1;
		PreparedStatement ps = dbc.prepareStatement(sql);
		ps.setInt(index++, zhyid);// zhyid 账页发生ID
		ps.setInt(index++, serialnum);// SerialNum 排列号
		ps.setString(index++, hz.getEntid());// EntId 企业ID
		ps.setString(index++, shpid);// shpid 商品ID
		ps.setString(index++, hwid);// hwid 位置ID
		ps.setString(index++, wdid);// wdid 维度ID
		ps.setInt(index++, mx.getBillsn());// BillSn 单据序号
		ps.setString(index++, hz.getSummary());// Summaries 摘要
		ps.setDouble(index++, 0d);// rukjs
		// 入库件数
		ps.setDouble(index++, 0d);// rukshl
		// 入库数量
		ps.setDouble(index++, 0d);// rkdj
		// 入库单价
		ps.setDouble(index++, 0d);// rkje 入库金额
		ps.setDouble(index++, 0d);// chkjs
		// 出库件数
		ps.setDouble(index++, 0d);// chkshl
		// 出库数量
		ps.setDouble(index++, kcje);// chkje
		// 出库金额
		ps.setDouble(index++, jcParams[0][0]);// hwjcjs 货位结存件数
		ps.setDouble(index++, jcParams[0][1]);// hwjcshl 货位结存数量
		ps.setDouble(index++, jcParams[0][2]);// hwjcje 货位结存金额
		ps.setDouble(index++, hwjcdj);// hwjcdj 货位结存单价
		ps.setDouble(index++, jcParams[1][0]);// orgjcjs 机构结存件数
		ps.setDouble(index++, jcParams[1][1]);// orgjcshl 机构结存数量
		ps.setDouble(index++, jcParams[1][2]);// orgjcje 机构结存金额
		ps.setDouble(index++, orgjcdj);// orgjcdj 机构结存单价
		ps.setDouble(index++, jcParams[3][0]);// jianshu 总结存件数
		ps.setDouble(index++, jcParams[3][1]);// kcshl 总结存数量
		ps.setDouble(index++, jcParams[3][2]);// kcje 总结存金额
		ps.setDouble(index++, entjcdj);// jcdj 总结存单价
		ps.setDouble(index++, 0d);// hsje
		// 含税金额
		ps.setDouble(index++, 0d);// je
		// 不含税金额
		ps.setDouble(index++, 0d);// lshje
		// 零售金额
		ps.setDouble(index++, kcje * -1);// maol 毛利
		ps.setString(index, "'库存为0自动补出库成本");// Remark 备注
		ps.execute();
		ps.close();
	}

	private void insertShpzymx(Database dbc, double jcParams[][], int zhyid, String shpid, String hwid, String wdid) throws Exception {
		String sql = "INSERT shpzymx (hyid,serialnum,entid,shpid,hwid,wdid,billsn,summaries,rukjs,rukshl,rkdj,rkje,chkjs,chkshl,chkje,hwjcjs,hwjcshl,hwjcje,hwjcdj,orgjcjs,orgjcshl,orgjcje,orgjcdj,jianshu,kcshl,kcje,jcdj,hsje,je,lshje,maol,remark) VALUES (?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?)";
		double hwjcdj = 0d;
		double orgjcdj = 0d;
		double entjcdj = 0d;
		if (!isZore(jcParams[0][1])) {
			hwjcdj = jcParams[0][2] / jcParams[0][1];
		}
		if (!isZore(jcParams[1][1])) {
			orgjcdj = jcParams[1][2] / jcParams[1][1];
		}
		if (!isZore(jcParams[3][1])) {
			entjcdj = jcParams[3][2] / jcParams[1][1];
		}
		int serialnum = 0;
		int index = 1;
		PreparedStatement ps = dbc.prepareStatement(sql);
		ps.setInt(index++, zhyid);// zhyid 账页发生ID
		ps.setInt(index++, serialnum);// SerialNum 排列号
		ps.setString(index++, hz.getEntid());// EntId 企业ID
		ps.setString(index++, shpid);// shpid 商品ID
		ps.setString(index++, hwid);// hwid 位置ID
		ps.setString(index++, wdid);// wdid 维度ID
		ps.setInt(index++, mx.getBillsn());// BillSn 单据序号
		ps.setString(index++, hz.getSummary());// Summaries 摘要
		ps.setDouble(index++, mx.getRukujianshu());// rukjs
		// 入库件数
		ps.setDouble(index++, mx.getRukushul());// rukshl
		// 入库数量
		ps.setDouble(index++, mx.getRukujianshu());// rkdj
		// 入库单价
		ps.setDouble(index++, mx.getRukujine());// rkje 入库金额
		ps.setDouble(index++, mx.getCukujianshu());// chkjs
		// 出库件数
		ps.setDouble(index++, mx.getCukushul());// chkshl
		// 出库数量
		ps.setDouble(index++, mx.getCukujine());// chkje
		// 出库金额
		ps.setDouble(index++, jcParams[0][0]);// hwjcjs 货位结存件数
		ps.setDouble(index++, jcParams[0][1]);// hwjcshl 货位结存数量
		ps.setDouble(index++, jcParams[0][2]);// hwjcje 货位结存金额
		ps.setDouble(index++, hwjcdj);// hwjcdj 货位结存单价
		ps.setDouble(index++, jcParams[1][0]);// orgjcjs 机构结存件数
		ps.setDouble(index++, jcParams[1][1]);// orgjcshl 机构结存数量
		ps.setDouble(index++, jcParams[1][2]);// orgjcje 机构结存金额
		ps.setDouble(index++, orgjcdj);// orgjcdj 机构结存单价
		ps.setDouble(index++, jcParams[3][0]);// jianshu 总结存件数
		ps.setDouble(index++, jcParams[3][1]);// kcshl 总结存数量
		ps.setDouble(index++, jcParams[3][2]);// kcje 总结存金额
		ps.setDouble(index++, entjcdj);// jcdj 总结存单价
		ps.setDouble(index++, mx.getHansuijine());// hsje
		// 含税金额
		ps.setDouble(index++, mx.getBuhansuijine());// je
		// 不含税金额
		ps.setDouble(index++, mx.getLinsoujine());// lshje
		// 零售金额
		ps.setDouble(index++, mx.getMaoli());// maol 毛利
		ps.setString(index, hz.getRemark());// Remark 备注
		ps.execute();
		ps.close();
	}

	/**
	 * 
	 * @param param1
	 * @param param2
	 * @return 一个double类型的数组（长度是7）<br>
	 * 
	 *         0. 结存件数<br>
	 *         1.结存数量 <br>
	 *         2.结存金额 <br>
	 *         3.核算成本单价 <br>
	 *         4.最高进价<br>
	 *         5.最低进价<br>
	 *         6.最后进价<br>
	 */
	private double[] getJcParameters(Database dbc, String param1, String param2, int type) throws Exception {
		double[] param = new double[] { 0d, 0d, 0d, 0d, 0d, 0d, 0d };
		String sql;
		switch (type) {
		case (0):// 货位
			sql = "SELECT kcjs,ckshl,kcje,hescbj FROM shphwjc WHERE hwid=? AND shpid=? AND ownerid = ?";
			break;
		case (1):// 机构
			sql = "SELECT  kcjs,ckshl, kcje,hescbj,zgjj,zdjj,zhjj FROM shporgjc WHERE orgid=? AND shpid=?";
			break;
		case (2):// 成本中心
			sql = "SELECT  kcjs,ckshl, kcje,hescbj,zgjj,zdjj,zhjj FROM shphszx WHERE orgid=? AND shpid=?";
			break;
		case (3):// 总结存
			sql = "SELECT  kcjs,ckshl, kcje,hescbj,zgjj,zdjj,zhjj FROM shphszx WHERE entid=? AND shpid=?";
			break;
		default:// 货位
			return param;
		}
		PreparedStatement ps = dbc.prepareStatement(sql);
		ps.setString(1, param1);
		ps.setString(2, param2);
		if (type == 0) {
			ps.setString(3, ownerid);
		}
		ResultSet rst = ps.executeQuery();
		ResultSetMetaData rsmd = rst.getMetaData();
		int count = rsmd.getColumnCount();
		if (rst.next()) {
			for (int i = 0; i < count && i < 7; i++) {
				param[i] = rst.getDouble(i + 1);
			}
		}
		rst.close();
		ps.close();
		param[0] = param[0] + mx.getRukujianshu() - mx.getCukujianshu();
		param[1] = param[1] + mx.getRukushul() - mx.getCukushul();
		param[2] = param[3] + mx.getRukujine() - mx.getCukujine();
		return param;
	}

	public void updatejc(Database dbc, String itemId, String shpid, double jcparam[], int length, int type) throws Exception {
		String sql = null;
		switch (type) {
		case (0):// 货位
			sql = "UPDATE shphwjc SET kcjs=?,ckshl=?,kcje=?,hescbj=? WHERE hwid=? AND shpid=? AND ownerid=?";
			break;
		case (1):// 机构
			sql = " UPDATE shporgjc SET kcjs=?,ckshl=?, kcje=?,hescbj=?,zgjj=?,zdjj=?,zhjj=? WHERE orgid=? AND shpid=?";
			break;
		case (2):// 成本中心
			sql = "UPDATE shphszx SET kcjs=?,ckshl=?, kcje=?,hescbj=?,zgjj=?,zdjj=?,zhjj=? WHERE orgid=? AND shpid=?";
			break;
		case (3):// 总结存
			sql = "UPDATE shphszx SET kcjs=?,ckshl=?, kcje=?,hescbj=?,zgjj=?,zdjj=?,zhjj=? WHERE entid=? AND shpid=?";
			break;
		default:// 货位
			return;
		}
		PreparedStatement ps = dbc.prepareStatement(sql);
		int index = 1;
		for (int i = 0; i < length; i++) {
			ps.setDouble(index++, jcparam[i]);
		}
		ps.setString(index++, itemId);
		ps.setString(index++, shpid);
		if (type == 0) {
			ps.setString(index++, ownerid);
		}
		int updateCount = ps.executeUpdate();
		if (updateCount == 0) {
			// TODO 说明上没有写明白 对应317行
		}
		ps.close();
	}

	private void updateShpbzjc(Database dbc, String shpid, String hwid, String baozhid) throws Exception {
		String sql = "UPDATE shpbzjc SET ckshl=ckshl+? WHERE shpid=? AND hwid=? AND baozhid=? AND ownerid=?";
		PreparedStatement ps = dbc.prepareStatement(sql);
		ps.setDouble(1, mx.getRukushul() - mx.getCukushul());
		ps.setString(2, shpid);
		ps.setString(3, hwid);
		ps.setString(4, baozhid);
		ps.setString(5, ownerid);
		ps.executeUpdate();
		ps.close();
	}

	private void insertShpbzjc(Database dbc, String shpid, String hwid, String baozhid) throws Exception {
		int jlgg = 1;
		String sql = "SELECT jlgg FROM shpbzgx WHERE shpid=? AND baozhid=?";
		PreparedStatement ps = dbc.prepareStatement(sql);
		ps.setString(1, shpid);
		ps.setString(2, baozhid);
		ResultSet rst = ps.executeQuery();
		if (rst.next()) {
			jlgg = rst.getInt(1);
		}
		rst.close();
		ps.close();
		sql = "INSERT shpbzjc(hwid,shpid,baozhid,EntId,jlgg,kcshl,ownerid)VALUES(?,?,?,?,?, ?,?)";
		ps = dbc.prepareStatement(sql);
		ps.setString(1, hwid);
		ps.setString(2, shpid);
		ps.setString(3, baozhid);
		ps.setString(4, hz.getEntid());
		ps.setInt(5, jlgg);
		ps.setDouble(6, mx.getRukushul());
		ps.setString(7, ownerid);
		ps.execute();
		ps.close();
	}

	private void update(Database dbc, String wdid, double fentanshul, String spid, String hwid, boolean isZero) throws Exception {
		/*
		 * update 商品维度结存管理(AC010109) set 库存数量=库存数量 - :v_分摊数量 where
		 * 存货位置ID=:存货位置ID and 商品ID=:商品ID and 维度ID=:v_分摊批号数据集.维度ID insert into
		 * 自动分摊批号(AC010407) (单据ID，单据序号，维度ID，商品ID，存货位置ID，单据编号，分摊数量，企业ID，分摊标识)
		 * values (:单据ID，:单据序号，:维度ID，:商品ID，:存货位置ID，:单据编号，:v_分摊数量，:企业ID，'B')
		 * v_分摊数量:=0
		 */
		String sql;
		if (isZero) {
			sql = "UPDATE shpwdjc SET kcshl=?  WHERE  hwid=? AND shpid=? AND wdid=? AND ownerid =?";
		} else {
			sql = "UPDATE shpwdjc SET kcshl=kcshl -?  WHERE  hwid=? AND shpid=? AND wdid=?  AND ownerid =?";
		}
		PreparedStatement ps = dbc.prepareStatement(sql);
		ps.setDouble(1, fentanshul);
		ps.setString(2, hwid);
		ps.setString(3, spid);
		ps.setString(4, wdid);
		ps.setString(4, ownerid);
		ps.executeUpdate();
		ps.close();
		insertAutopihao(dbc, spid, hwid, wdid, fentanshul);
	}

	private void insertShpwdjc(Database dbc, String shpid, String hwid, String wdid, String shengcrq, double gebjj, double kcjs, double kcshl) throws SQLException {
		String sql = "INSERT AngleBalance (hwid,shpid,wdid,EntId,shengcrq,gebjj,kcjs,kcshl,ownerid) VALUES(?,?,?,?,?, ?,?,?,?)";
		int index = 1;
		PreparedStatement ps = dbc.prepareStatement(sql);
		ps.setString(index++, hwid);
		ps.setString(index++, shpid);
		ps.setString(index++, wdid);
		ps.setString(index++, hz.getEntid());
		ps.setString(index++, shengcrq);
		ps.setDouble(index++, gebjj);
		ps.setDouble(index++, kcjs);
		ps.setDouble(index++, kcshl);
		ps.setString(index++, ownerid);
		ps.execute();
		ps.close();
	}

	private void updateShpwdjc(Database dbc, String shpid, String hwid, String wdid, double rkjs, double ckjs, double kcshl) throws Exception {
		String sql = "UPDATE AngleBalance SET kcjs = kcjs + ? - ?, kcshl=kcsl + ? -? WHERE hwid=? AND shpid=? AND wdid=? AND ownerid=?";
		int index = 1;
		PreparedStatement ps = dbc.prepareStatement(sql);
		ps.setDouble(index++, mx.getRukujianshu());
		ps.setDouble(index++, mx.getCukujianshu());
		ps.setDouble(index++, mx.getRukushul());
		ps.setDouble(index++, mx.getCukushul());
		ps.setString(index++, hwid);
		ps.setString(index++, shpid);
		ps.setString(index++, wdid);
		ps.setString(index++, ownerid);
		ps.execute();
		ps.close();
	}

	private void insertAutopihao(Database dbc, String shpid, String hwid, String wdid, double fentanshul) throws Exception {
		String sql = "INSERT autopihao (billno,billsn,wdid,entid,shpid,hwid,billcode,shl,flag,ownerid) VALUES(?,?,?,?,?, ?,?,?,?,?)";
		PreparedStatement ps = dbc.prepareStatement(sql);
		ps.setInt(1, hz.getBillno());
		ps.setInt(2, mx.getBillsn());
		ps.setString(3, wdid);
		ps.setString(4, hz.getEntid());
		ps.setString(5, shpid);
		ps.setString(6, hwid);
		ps.setString(7, hz.getBillcode());
		ps.setDouble(8, fentanshul);
		ps.setString(9, "B");
		ps.setString(10, ownerid);
		ps.execute();
		ps.close();
	}

	// 获取商品的核算方式
	private int getHsfs() {
		return 1;
	}
}
